package com.cskaoyan.service.footprint_service;

import com.cskaoyan.bean.FootPrint;
import com.cskaoyan.bean.FootPrintExample;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GoodsExample;
import com.cskaoyan.bean.footprintVO.FootPrintVO;
import com.cskaoyan.bean.footprintVO.FootprintListBean;
import com.cskaoyan.mapper.FootPrintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 陈泉江
 * @version 1.0
 * @description: TODO
 * @date 2021/6/7 15:55
 */

@Service
public class FootPrintServiceImpl implements FootPrintService {

    @Autowired
    FootPrintMapper footPrintMapper;

    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public FootPrintVO list(Integer userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);

        FootPrintExample footPrintExample = new FootPrintExample();
        FootPrintExample.Criteria criteria = footPrintExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        footPrintExample.setOrderByClause("add_time desc");
        //根据用户id获得所有足迹表
        List<FootPrint> footPrints = footPrintMapper.selectByExample(footPrintExample);
        List<FootprintListBean> footprintListBeans = new ArrayList<>();

        LinkedHashMap<Integer, Object> map = new LinkedHashMap<>();
        for (FootPrint footPrint : footPrints) {

            if (!map.containsKey(footPrint.getGoodsId())) {
                FootprintListBean footprintListBean = new FootprintListBean();

                //获得足迹表关联的商品表
                Goods goods = goodsMapper.selectByPrimaryKey(footPrint.getGoodsId());
                Date addTime = footPrint.getAddTime();
                String format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(addTime);
                footprintListBean.setAddTime(format);
                footprintListBean.setBrief(goods.getBrief());
                footprintListBean.setGoodsId(footPrint.getGoodsId());
                footprintListBean.setId(footPrint.getId());
                footprintListBean.setName(goods.getName());
                footprintListBean.setPicUrl(goods.getPicUrl());
                footprintListBean.setRetailPrice(goods.getRetailPrice());

                footprintListBeans.add(footprintListBean);
            }

            map.put(footPrint.getGoodsId(), footPrint);
        }

        PageInfo<FootPrint> printPageInfo = new PageInfo<>(footPrints);
        long total = printPageInfo.getTotal();

        FootPrintVO footPrintVO = new FootPrintVO();
        footPrintVO.setFootprintList(footprintListBeans);
        footPrintVO.setTotalPages(total);

        return footPrintVO;
    }

    @Override
    public int footprintDel(Integer id) {
        int code = footPrintMapper.updateDelStatus(id);
        return code;
    }
}
